package com.crois.barrier.admin.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.crois.barrier.admin.entity.PluginData;
import com.crois.barrier.admin.mapper.PluginDataMapper;
import com.crois.barrier.common.exception.BarrierException;
import com.crois.barrier.common.exception.BarrierExceptionEnum;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
import java.util.prefs.BackingStoreException;

/**
 * barrier 插件数据表
 * @author Hou Ze Yu
 * @date 2021-10-25 11:58:28
 */
@Slf4j
@Service
@Transactional(rollbackFor = Exception.class)
public class PluginDataSvc extends ServiceImpl<PluginDataMapper,PluginData> implements IPluginDataSvc {


    @Autowired
    @Qualifier("nacosConfigSvc")
    private INaocsConfigSvc naocsConfigSvc;


    @Override
    public List<PluginData> findPluginList(String pluginName) {
        QueryWrapper<PluginData> dataQueryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(pluginName)){
            dataQueryWrapper.lambda().like(PluginData::getName,"%"+pluginName+"%");
        }
        return baseMapper.selectList(dataQueryWrapper);
    }

    @Override
    public List<PluginData> findRoutePluginList(Integer routeId) {
        return baseMapper.selectRoutePluginList(routeId);
    }

    @Override
    public void saveRoutePlugin(Integer routeId, Integer pluginId) {
      int result = baseMapper.saveRoutePlugin(routeId,pluginId);
        if (result==0){
            throw new BarrierException(BarrierExceptionEnum.BARRIER_GATEWAY_ROUTE_UPDATE_FAIL);
        }

        naocsConfigSvc.pushApiRoute();
    }

    @Override
    public void deleteRoutePlugin(Integer routeId, Integer pluginId) {
        int result = baseMapper.deleteRoutePlugin(routeId,pluginId);
        if (result==0){
          throw new BarrierException(BarrierExceptionEnum.BARRIER_GATEWAY_ROUTE_UPDATE_FAIL);
        }
        naocsConfigSvc.pushApiRoute();
    }

    @Override
    public void savePlugin(PluginData pluginData) {
        int result = baseMapper.insert(pluginData);
        if (result==0){
            throw new BarrierException(BarrierExceptionEnum.BARRIER_GATEWAY_PLUGIN_ADD_FAIL);
        }
        naocsConfigSvc.pushPlugin();
    }

    @Override
    public void updatePlugin(PluginData pluginData) {
        int result = baseMapper.updateById(pluginData);
        if (result==0){
            throw new BarrierException(BarrierExceptionEnum.BARRIER_GATEWAY_PLUGIN_UPDATE_FAIL);
        }
        naocsConfigSvc.pushPlugin();
    }

    @Override
    public void removePlugin(Integer pluginId) {
        int result = baseMapper.deleteById(pluginId);
        if (result==0){
            throw new BarrierException(BarrierExceptionEnum.BARRIER_GATEWAY_PLUGIN_UPDATE_FAIL);
        }
        naocsConfigSvc.pushPlugin();
    }
}
